package com.xl.tools;

import org.apache.commons.compress.utils.Lists;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class ZnycQrCode {

    public static void main(String[] args) throws IOException {
        String regex = "(\\d+).png";
        Pattern pattern = Pattern.compile(regex);

        List<String> list = Lists.newArrayList();

        File dir = new File("D:/tmp/znyc");
        Arrays.asList(dir.listFiles()).stream().filter(item -> {
            return item.isDirectory();
        }).forEach(item -> {
            Arrays.asList(item.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".png");
                }
            })).stream().forEach(subItem -> {
                String name = subItem.getName().toLowerCase();
                Matcher matcher = pattern.matcher(name);
                if (matcher.find()) {
                    list.add(matcher.group(1));
                }
            });
        });
        Files.write(Paths.get("D:", "tmp", "qrcode.txt"), list.stream().collect(Collectors.joining("\n")).getBytes(StandardCharsets.UTF_8));
    }
}
